package com.tang.framework.core.facade;

import com.tang.framework.component.TokenService;
import com.tang.framework.core.domain.HttpResult;
import com.tang.framework.core.model.LoginUser;
import com.tang.common.exception.eumes.Error;
import org.springframework.context.MessageSource;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * @Author tang jun liang
 * @Date 2023/5/7 22:30
 */
public class Facade {

    @Resource
    protected HttpServletRequest request;


    @Resource
    protected HttpServletResponse response;


    @Resource
    protected MessageSource messageSource;

    @Resource
    private TokenService tokenService;


    protected LoginUser getCurrLoginUser() {
        return tokenService.getLoginUser(request);
    }


    protected <T> HttpResult<T> ok(T data) {
        return HttpResult.ok(data);
    }

    protected <T> HttpResult<T> ok() {
        return HttpResult.ok();
    }

    protected <T> HttpResult<T> error(Integer code, String message, Object... args) {
        return HttpResult.error(code, messageSource.getMessage(message, args, request.getLocale()));
    }

    protected <T> HttpResult<T> error(String message, Object... args) {
        return HttpResult.error(messageSource.getMessage(message, args, request.getLocale()));
    }

    protected <T> HttpResult<T> error(Error.ErrorCodeMessage errorCodeMessage) {
        return HttpResult.error(errorCodeMessage);
    }
}
